<template>
  <el-dialog
    title="添加经纪"
    :visible="isshowDialog"
    @close="btnCancel"
  >
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">

      <el-form-item label="经纪" prop="name">
        <el-select v-model="agent" placeholder="全部经纪">
          <el-option
            v-for="item in formInline"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model="ruleForm.wechat" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <ImageUploads ref="addImage" :file-image="fileImage" />
      </el-form-item>

    </el-form>
    <el-row slot="footer" type="flex" justify="end">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btOk(editId)">确认</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import ImageUploads from '@/components/ImageUploads/index.vue'
import { postClientsList, idClientsList, putClientsList } from '@/api/clients'
export default {
  components: {
    ImageUploads
  },
  props: {
    isshowDialog: {
      type: Boolean,
      required: false
    },
    formInline: {
      type: Array,
      required: true
    },
    editId: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      agent: '',
      ruleForm: {},
      rules: {},
      fileImage: null
    }
  },
  watch: {
    async  editId() {
      if (this.editId > 0) {
        const res = await idClientsList(this.editId)
        this.ruleForm = res
        this.agent = res.agent
        this.fileImage = res.image
        this.$refs.addImage.fileList.push({ url: this.fileImage })
      }
    }

  },
  methods: {
    async btOk(editId) {
      if (editId > 0) {
        console.log(editId)
        await putClientsList(
          { ...this.ruleForm,
            agent: this.agent,
            image: this.$refs.addImage.fileList[0].url,
            id: editId
          })
        this.$emit('update:editId', -1)
      } else {
        await postClientsList({ ...this.ruleForm, agent: this.agent, image: this.$refs.addImage.fileList[0].url })
      }
      this.fileImage = null
      this.$refs.addImage.delImages()
      this.$emit('update:isshowDialog', false)
      this.$parent.clients()
    },
    btnCancel() {
      this.ruleForm = {}
      this.agent = ''
      this.$emit('update:isshowDialog', false)

      this.$emit('update:editId', -1)
      if (this.$refs.addImage) {
        this.fileImage = null
        console.log(this.$refs.addImage)
        this.$refs.addImage.delImages()
      }
    }
  }
}
</script>

<style>

</style>
